package cn.knight.org.service.impl;

import cn.knight.basic.query.BaseQuery;
import cn.knight.basic.service.impl.BaseServiceImpl;
import cn.knight.basic.utils.MD5Utils;
import cn.knight.basic.utils.PageList;
import cn.knight.basic.utils.StrUtils;
import cn.knight.org.domain.Employee;
import cn.knight.org.mapper.EmployeeMapper;
import cn.knight.org.service.IEmployeeService;
import cn.knight.system.domain.Role;
import cn.knight.system.mapper.RoleMapper;
import cn.knight.user.domain.Logininfo;
import cn.knight.user.mapper.LogininfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements IEmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private LogininfoMapper logininfoMapper;

    @Autowired

    private RoleMapper roleMapper;


    /**
    * @Title: queryRows
    * @Description: 分页展示数据
    * @Author: winnie
    * @Version: 1.0
    * @Date:  2022/7/13 20:35
    * @Parameters: [baseQuery]
    * @Return cn.knight.basic.utils.PageList<cn.knight.org.domain.Employee>
    */
    @Override
    public PageList<Employee> queryRows(BaseQuery baseQuery) {
        List<Employee> employees = employeeMapper.queryRows(baseQuery);
        for (Employee employee : employees) {
            List<Role> roles = roleMapper.loadByEmployeeId(employee.getId());
            employee.setOwnRoles(roles);
        }
        Integer count = employeeMapper.queryCount(baseQuery);
        return new PageList<>(count,employees);
    }

    /**
    * @Title: save
    * @Description: 同步新增logininfo数据
    * @Author: winnie
    * @Version: 1.0
    * @Date:  6/7/2022 下午6:25
    * @Parameters: [employee]
    * @Return void
    */
    @Override
    public void save(Employee employee) {
        // 员工信息入库
        initEmployee(employee);
        //登录表信息入库
        Logininfo logininfo = employee2Logininfo(employee);
        logininfoMapper.save(logininfo);
        // 修改员工的logininfo_id
        employee.setLogininfoId(logininfo.getId());
        employeeMapper.save(employee);
        // 初始化中间表
        initsaveRelationTable(employee);
    }

    private void initsaveRelationTable(Employee employee) {
        if (employee.getRoles().size() > 0){
            // 根据角色表的id和菜单&权限的数组添加数据到中间表中
            employeeMapper.saveEmployeeRole(employee.getId(),employee.getRoles());

        }
    }

    /**
    * @Title: update
    * @Description: 同步修改logininfo数据
    * @Author: winnie
    * @Version: 1.0
    * @Date:  6/7/2022 下午6:44
    * @Parameters: [employee]
    * @Return void
    */
    @Override
    public void update(Employee employee) {
        initEmployee(employee);
        employeeMapper.update(employee);
        Logininfo logininfo = employee2Logininfo(employee);
        logininfoMapper.update(logininfo);
        initDelRelationTable(employee.getId());
        initsaveRelationTable(employee);
    }

    /**
    * @Title: remove
    * @Description: 同步删除logininfo数据
    * @Author: winnie
    * @Version: 1.0
    * @Date:  6/7/2022 下午6:25
    * @Parameters: [id]
    * @Return void
    */
    @Override
    public void remove(Long id) {
        Employee emp = employeeMapper.loadById(id);
        employeeMapper.remove(id);
        logininfoMapper.remove(emp.getLogininfoId());
        initDelRelationTable(id);

    }

    private void initDelRelationTable(Long id) {
        employeeMapper.removeEmployeeRole(id);
    }


    /**
     * @Title: initEmployee
     * @Description: 初始化employee对象中的字段值
     * @Author: winnie
     * @Version: 1.0
     * @Date:  6/7/2022 下午6:48
     * @Parameters: [employee]
     * @Return void
     */
    private void initEmployee(Employee employee) {
        String salt = StrUtils.getComplexRandomString(32);
        String saltPassword = MD5Utils.encrypByMd5(salt + employee.getPassword());
        employee.setSalt(salt);
        employee.setPassword(saltPassword);
    }

    /**
     * @Title: employee2Logininfo
     * @Description: 将employee对象中的字段值拷贝到logininfo对象中
     * @Author: winnie
     * @Version: 1.0
     * @Date:  6/7/2022 下午6:48
     * @Parameters: [employee]
     * @Return cn.knight.user.domain.Logininfo
     */
    private Logininfo employee2Logininfo(Employee employee) {
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(employee, logininfo);
        logininfo.setType(0);
        logininfo.setId(employee.getLogininfoId());
        return logininfo;


    }


}
